package cn.iocoder.yudao.module.alipay.service.sensitivenems;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.alipay.controller.admin.sensitivenems.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.sensitivenems.SensitiveNemsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.sensitivenems.SensitiveNemsConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.sensitivenems.SensitiveNemsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * 敏感词 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SensitiveNemsServiceImpl implements SensitiveNemsService {

    @Resource
    private SensitiveNemsMapper sensitiveNemsMapper;

    @Override
    public Integer createSensitiveNems(SensitiveNemsCreateReqVO createReqVO) {
        // 插入
        SensitiveNemsDO sensitiveNems = SensitiveNemsConvert.INSTANCE.convert(createReqVO);
        sensitiveNemsMapper.insert(sensitiveNems);
        // 返回
        return sensitiveNems.getId();
    }

    @Override
    public void updateSensitiveNems(SensitiveNemsUpdateReqVO updateReqVO) {
        // 校验存在
        validateSensitiveNemsExists(updateReqVO.getId());
        // 更新
        SensitiveNemsDO updateObj = SensitiveNemsConvert.INSTANCE.convert(updateReqVO);
        sensitiveNemsMapper.updateById(updateObj);
    }

    @Override
    public void deleteSensitiveNems(Integer id) {
        // 校验存在
        validateSensitiveNemsExists(id);
        // 删除
        sensitiveNemsMapper.deleteById(id);
    }

    private void validateSensitiveNemsExists(Integer id) {
        if (sensitiveNemsMapper.selectById(id) == null) {
            throw exception(SENSITIVE_NEMS_NOT_EXISTS);
        }
    }

    @Override
    public SensitiveNemsDO getSensitiveNems(Integer id) {
        return sensitiveNemsMapper.selectById(id);
    }

    @Override
    public List<SensitiveNemsDO> getSensitiveNemsList(Collection<Integer> ids) {
        return sensitiveNemsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SensitiveNemsDO> getSensitiveNemsPage(SensitiveNemsPageReqVO pageReqVO) {
        return sensitiveNemsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SensitiveNemsDO> getSensitiveNemsList(SensitiveNemsExportReqVO exportReqVO) {
        return sensitiveNemsMapper.selectList(exportReqVO);
    }

}
